package com.axx.android.subject;

import android.app.Activity;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.axx.android.R;
import com.axx.android.moments.ImageAdapter;
import com.axx.android.moments.ImageModel;
import com.axx.android.utils.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.previewlibrary.PhotoActivity;
import com.previewlibrary.ThumbViewInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/3 0003.
 */

public class SubjectNowAdapter extends BaseQuickAdapter<SubjectNow.SubjectDetailListBean, BaseViewHolder> {
    public SubjectNowAdapter(@LayoutRes int layoutResId, @Nullable List<SubjectNow.SubjectDetailListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, SubjectNow.SubjectDetailListBean item) {
        helper.setText(R.id.tv_title, item.getUser().getNickName());
        helper.setText(R.id.tv_content, item.getText());
        helper.setText(R.id.tv_up_cnt, item.getGood() + "");
        helper.setText(R.id.tv_time, getTime(item.getTime()));
        ImageLoader.loadRoundImage(item.getUser().getHeadPic(), (ImageView) helper.getView(R.id.iv_image), mContext);
        helper.addOnClickListener(R.id.iv_image);
        helper.addOnClickListener(R.id.iv_up);

        ImageView imageView = helper.getView(R.id.iv_up);
        if (item.getGoodStatus() == 1){
            imageView.setSelected(true);
        }else{
            imageView.setSelected(false);
        }

        RecyclerView recyclerView = helper.getView(R.id.rv_image);
        if (item.getPic() != null && item.getPic().size() != 0) {
            List<ImageModel> list = new ArrayList<>();
            final ArrayList<ThumbViewInfo> thumbViewInfos = new ArrayList<>();
            for (SubjectNow.SubjectDetailListBean.ImageBean imageBean : item.getPic()) {
                ImageModel imageModel = new ImageModel();
                imageModel.setAdd(false);
                imageModel.setCanDel(false);
                imageModel.setUrl(imageBean.getUrl());
                list.add(imageModel);

                ThumbViewInfo thumbViewInfo = new ThumbViewInfo(imageBean.getUrl());
                thumbViewInfos.add(thumbViewInfo);
            }

            recyclerView.setVisibility(View.VISIBLE);
            ImageAdapter imageAdapter = new ImageAdapter(R.layout.view_item_image, list);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
            recyclerView.setAdapter(imageAdapter);
            imageAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    PhotoActivity.startActivity((Activity) mContext,thumbViewInfos,position);

                }
            });
        } else {
            recyclerView.setVisibility(View.GONE);
        }

    }

    private String getTime(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            int year = calendar.get(Calendar.YEAR);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            calendar.setTime(new Date());
            int curYear = calendar.get(Calendar.YEAR);
            int curDay = calendar.get(Calendar.DAY_OF_MONTH);

            if (year == curYear && day == curDay){
                return new SimpleDateFormat("HH:mm").format(date);
            }else{
                return new SimpleDateFormat("MM-dd HH:mm").format(date);
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }
}
